package com.icesoft.xsnow.framework.config.orm;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.icesoft.xsnow.common.core.constant.CommonConstants;
import com.icesoft.xsnow.common.security.util.SecurityUtils;
import com.icesoft.xsnow.framework.config.MybatisPlusAutoConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;

import java.time.LocalDateTime;

/**
 * @program: xsnow
 * @description: 公共字段自动填充
 * @author: xuefeng.gao
 * @create: 2019-05-16 16:47
 **/
@Slf4j
@ConditionalOnBean(MybatisPlusAutoConfiguration.class)
@AutoConfigureAfter(MybatisPlusAutoConfiguration.class)
public class CommonFieldsAutomaticHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("start insert fill ....");
        this.setInsertFieldValByName(CommonConstants.COMMON_FIELD_CREATE_DATE, LocalDateTime.now(),metaObject);
        this.setInsertFieldValByName(CommonConstants.COMMON_FIELD_UPDATE_DATE, LocalDateTime.now(),metaObject);
        this.setInsertFieldValByName(CommonConstants.COMMON_FIELD_CREATE_UID, SecurityUtils.getUser().getId(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("start update fill ....");
        this.setInsertFieldValByName(CommonConstants.COMMON_FIELD_UPDATE_DATE, LocalDateTime.now(),metaObject);
    }
}
